﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DevComponents.DotNetBar

Public Class frmXuatHang
    Private m_QuyDinh As New QuyDinh()
    Private m_XuatHangCtrl As New XuatHangCtrl()
    Private m_MatHangCtrl As New MatHangCtrl()
    Private m_LoaiTienCtrl As New LoaiTienCtrl()
    Private m_LoaiGiaoDichCtrl As New LoaiGiaoDichCtrl()
    Private m_KhachHangCtrl As New KhachHangCtrl()
    Private m_NhanVienCtrl As New NhanVienCtrl()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub

    Private Sub frmXuatHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_KhachHangCtrl.HienThiComboBox(cmbTenKH)
        m_KhachHangCtrl.HienThiDataGridViewComboBoxColumn(colTenKH)
        m_NhanVienCtrl.HienThiComboBox(cmbTenNV)
        m_NhanVienCtrl.HienThiDataGridViewComboBoxColumn(colTenNV)
        m_MatHangCtrl.HienThiComboBox(cmbTenMH)
        m_MatHangCtrl.HienThiDataGridViewComboBoxColumn(colTenMH)
        m_XuatHangCtrl.HienThi(DGVXuatHang, BindingNavigatorXuatHang, txtMaPhieu, dtNgayXuat, txtSoLuong, txtGiaBan, _
         txtThanhTien, cmbTenMH, cmbTenKH, cmbTenNV, cmbLoaiGiaoDich, cmbLoaiTienGui)
    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_KhachHangCtrl.HienThiComboBox(cmbTenKH)
        m_KhachHangCtrl.HienThiDataGridViewComboBoxColumn(colTenKH)
        m_NhanVienCtrl.HienThiComboBox(cmbTenNV)
        m_NhanVienCtrl.HienThiDataGridViewComboBoxColumn(colTenNV)
        m_MatHangCtrl.HienThiComboBox(cmbTenMH)
        m_MatHangCtrl.HienThiDataGridViewComboBoxColumn(colTenMH)
        m_XuatHangCtrl.HienThi(DGVXuatHang, BindingNavigatorXuatHang, txtMaPhieu, dtNgayXuat, txtSoLuong, txtGiaBan, _
         txtThanhTien, cmbTenMH, cmbTenKH, cmbTenNV, cmbLoaiGiaoDich, cmbLoaiTienGui)
    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVXuatHang.RowCount = 0 Then
            BindingNavigatorXuatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_XuatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "XH" & m_QuyDinh.LaySTT(DGVXuatHang.Rows.Count + 1)
        m_Row("MaMH") = ""
        m_Row("MaKH") = ""
        m_Row("MaNV") = ""
        m_Row("NgayXuat") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("GiaBan") = 0
        m_Row("ThanhTien") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""

        m_XuatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorXuatHang.BindingSource.MoveLast()

    End Sub

    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVXuatHang.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colTenKH") = True AndAlso KiemTraTruocKhiLuu("colTenNV") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colThanhTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayXuat") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_XuatHangCtrl.LuuXuatHang()
        Else
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If

    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVXuatHang.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorXuatHang.BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()

    End Sub

    Private Sub btnThemMH_Click(sender As System.Object, e As System.EventArgs) Handles btnThemMH.Click
        LoadMain.HienThiMatHang()

    End Sub

    Private Sub btnThemKH_Click(sender As System.Object, e As System.EventArgs) Handles btnThemKH.Click
        LoadMain.HienThiKhachHang()

    End Sub

    Private Sub btnThemNV_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNV.Click
        LoadMain.HienThiNhanVien()

    End Sub

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()

    End Sub

    Private Sub btnNhapLai_Click(sender As System.Object, e As System.EventArgs) Handles btnNhapLai.Click
        txtGiaBan.Text = "0"
        txtSoLuong.Text = "0"
        txtThanhTien.Text = "0"

    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        If DGVXuatHang.RowCount = 0 Then
            BindingNavigatorXuatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_XuatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "XH" & m_QuyDinh.LaySTT(DGVXuatHang.Rows.Count + 1)
        m_Row("MaMH") = ""
        m_Row("MaKH") = ""
        m_Row("MaNV") = ""
        m_Row("NgayXuat") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("GiaBan") = 0
        m_Row("ThanhTien") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""
        m_XuatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorXuatHang.BindingSource.MoveLast()

    End Sub

    Private Sub btnLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnLuu.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colTenKH") = True AndAlso KiemTraTruocKhiLuu("colTenNV") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colThanhTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayXuat") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_XuatHangCtrl.LuuXuatHang()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Phải nhập đầy đủ dữ liệu để thêm đơn xuất hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If

    End Sub

    Private Sub btnSua_Click(sender As System.Object, e As System.EventArgs) Handles btnSua.Click
        m_XuatHangCtrl.SuaXuatHang(txtMaPhieu.Text, cmbTenMH.SelectedValue.ToString(), cmbTenKH.SelectedValue.ToString(), cmbTenNV.SelectedValue.ToString(), Convert.ToInt64(txtSoLuong.Text), Convert.ToInt64(txtGiaBan.Text), _
            Convert.ToInt64(txtThanhTien.Text), dtNgayXuat.Value, cmbLoaiTienGui.SelectedValue.ToString(), cmbLoaiGiaoDich.SelectedValue.ToString())
        m_XuatHangCtrl.HienThi(DGVXuatHang, BindingNavigatorXuatHang, txtMaPhieu, dtNgayXuat, txtSoLuong, txtGiaBan, _
         txtThanhTien, cmbTenMH, cmbTenKH, cmbTenNV, cmbLoaiGiaoDich, cmbLoaiTienGui)
        MessageBoxEx.Show("Đã sửa thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
        If MessageBoxEx.Show("Bạn có chắc chắn xóa đặt hàng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            m_XuatHangCtrl.XoaXuatHang(txtMaPhieu.Text, cmbTenMH.SelectedValue.ToString(), cmbTenKH.SelectedValue.ToString(), cmbTenNV.SelectedValue.ToString())
            m_XuatHangCtrl.HienThi(DGVXuatHang, BindingNavigatorXuatHang, txtMaPhieu, dtNgayXuat, txtSoLuong, txtGiaBan, _
             txtThanhTien, cmbTenMH, cmbTenKH, cmbTenNV, cmbLoaiGiaoDich, cmbLoaiTienGui)
        End If

    End Sub
End Class